package com.sgb.kjwl.viewmodel.material;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson3;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.repository.MaterialRepertory;
import com.sgb.kjwl.view.ui.fragment.material.MaterialListFragment4;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MaterialListFragModel4 extends BaseViewModel {
    public ObservableField<MaterialFilterJson3> materialFilterJson = new ObservableField<>(new MaterialFilterJson3());
    public ObservableField<String> areaCode = new ObservableField<>("");
    public ObservableField<Integer> type = new ObservableField<>(0);
    private MaterialListFragment4 fragment3;
    private MaterialRepertory repertory;
    private int page = 1;
    public int total;

    public MaterialListFragModel4(MaterialListFragment4 fragment3, MaterialRepertory repertory) {
        this.fragment3 = fragment3;
        this.repertory = repertory;
        areaCode.set(repertory.getAreaCode());
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }


    public void getMaterialModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        materialFilterJson.get().setPage(page);
        int size = 10;
        materialFilterJson.get().setSize(size);
        materialFilterJson.get().setAreaCode(areaCode.get());
        if (materialFilterJson.get().getPriceSort()==0) {
            materialFilterJson.get().setTimeSort(2);
        } else {
            materialFilterJson.get().setTimeSort(0);
        }

        if (type.get().intValue()==1){
            materialFilterJson.get().setPromoteStatus(2);
        }
        DialogHelper.showProgressDialog(fragment3.getContext(), null, "获取信数据中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getMaterialList3(materialFilterJson.get()).subscribe(listResponseBaseEntity -> {
            DialogHelper.dismissProgressDialog();
            if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                fragment3.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
            }
        }, throwable -> {
            DialogHelper.dismissProgressDialog();
            MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
        });
    }

    public void getFilterNameModel(String id,boolean isShowPop) {
        repertory.getMaterialFilterName(id, areaCode.get()).subscribe(listBaseEntity -> {
                    if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                        fragment3.showNamePopWindow(listBaseEntity.getData(),isShowPop);
                    }
                }, throwable -> MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage()));
    }

    public void getFilterStoreModel(boolean isShowPop) {
        repertory.getMaterialFilterStore(areaCode.get()).subscribe(listBaseEntity -> {
                    if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                        fragment3.showStorePopWindow(listBaseEntity.getData(),isShowPop);
                    }
                }, throwable -> MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage()));
    }

    public void getFilterTypeModel(String id) {
        repertory.getMaterialFilterType(id, areaCode.get()).subscribe(listBaseEntity -> {
                    if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                        fragment3.showTypePopWindow(listBaseEntity.getData());
                    }
                }, throwable -> MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage()));
    }

    public void getFilterPrice() {
        repertory.getMaterialPriceData().subscribe(listBaseEntity -> {
                    if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                        fragment3.showPricePopWindow(listBaseEntity.getData());
                    }
                }, throwable -> MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage()));
    }

    public void getFilterBrandModel(String id) {
        repertory.getMaterialFilterBrand(id, areaCode.get()).subscribe(listBaseEntity -> {
                    if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                        fragment3.showBrandPopWindow(listBaseEntity.getData());
                    }
                }, throwable -> MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage()));
    }

    public void getFilterBrandSpec(String id) {
        repertory.getMaterialFilterSpec(id, areaCode.get()).subscribe(listBaseEntity -> {
                    if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                        fragment3.showFilterBrandSpec(listBaseEntity.getData());
                    }
                }, throwable -> MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage()));
    }

    //商品列表 距离接口
    public void getFilterDistance(String cid1, int type) {
        DialogHelper.showProgressDialog(fragment3.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getMaterialDistanceData(cid1).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (type == 0) {
                        fragment3.showDistancePopWindow(response.body().getData(),true);
                    } else {
                        fragment3.showPop(response.body().getData());
                    }

                } else {
                    MToast.showToast(fragment3.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
